package cc.sjfx.sharedoc.dialog;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.mandroid.frame.toast.MToast;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.activity.BasicActivity;
import cc.sjfx.sharedoc.activity.BasicFragmentActivity;
import cc.sjfx.sharedoc.db.bmob.Examination;
import cc.sjfx.sharedoc.widget.anotation.ViewInject;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SearchDialog extends BasicFragmentActivity implements TextWatcher ,OnClickListener{
	@ViewInject(id=R.id.searchEdit)
	private EditText searchEdit;
	@ViewInject(id=R.id.searchBut)
	private Button searchBut;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_dialog);
		searchBut.setOnClickListener(this);
		searchEdit.setText(preference.getString("searchKey"));
		searchEdit.addTextChangedListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_dialog, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		preference.putString("searchKey", s.toString());
	}
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		String key=searchEdit.getText().toString();
		BmobQuery<Examination> query=new BmobQuery<Examination>();
		query.addWhereContains("title", key);
		query.findObjects(context, new FindListener<Examination>() {
			
			@Override
			public void onSuccess(List<Examination> arg0) {
				// TODO Auto-generated method stub
				if(arg0.size()!=0){
					MToast.show(context, "共找到"+arg0.size()+"条数据");
					ExamFilterDialog dialog=new ExamFilterDialog(context);
					dialog.addListData(arg0);
					dialog.show();
					preference.remove("searchKey");
				}else {
					MToast.show(context, "未找到相关信息");
				}
				
			}
			
			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}
		});
		
	//	finish();
	}
}
